import { getDicts, insertDict, delDict, editDict } from '@/api/basic'
const state = {
    dicts: {},
}
const getters = {
    //读取性别名称
    getDict: (state) => (code) => {
        if (code != null || code != undefined) {

            return state.dicts[code];
        } else {
            return state.dicts;
        }
    },

}
const mutations = {
    SET_DICTS(state, obj) {
        state.dicts = obj;
    },
    editOneBasic: (state, data) => {
        let arr = state.dicts[data.label].value
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].id == data.label.id) {
                arr[i] = data.value
            }
        }

        state.dicts[data.label].value = arr

    }

}
const actions = {
    // 获取字典库菜单列表  异步调用接口维护 需要调整
    getDict({ commit }, formData) {
        return getDicts(formData).then(res => {
            let dataList = res.data;
            let arr = []
            for (let index = 0; index < dataList.length; index++) {
                let obj = {}
                if (index != 0 && dataList[index].dict_type != dataList[index - 1].dict_type) {
                    obj.text = dataList[index].dict_type
                    obj.remark = dataList[index].remark
                    obj.value = [dataList[index]]
                    obj.obj = {}
                    obj.obj[dataList[index].dict_key] = dataList[index].dict_value
                    arr.push(obj)
                } else if (index != 0 && dataList[index].dict_type == dataList[index - 1].dict_type) {
                    for (let i = 0; i < arr.length; i++) {
                        if (arr[i].text == dataList[index].dict_type) {
                            arr[i].value.push(dataList[index])
                            arr[i].obj[dataList[index].dict_key] = dataList[index].dict_value

                        }
                    }

                } else {
                    obj.text = dataList[index].dict_type
                    obj.value = [dataList[index]]
                    obj.remark = dataList[index].remark
                    obj.obj = {}
                    obj.obj[dataList[index].dict_key] = dataList[index].dict_value
                    arr.push(obj)
                }
            }
            commit("SET_DICTS", arr);
            return res.data

        })
    },
    // 插入字典库菜单列表
    insertDict({ commit }, formData) {
        return insertDict(formData).then((res) => {
            if (res && res.data) {
                commit("SET_DICTS", res.data);
                return res;
            }
        });
    },
    // 删除字典库菜单列表 
    delDict({ commit }, formData) {

        return delDict(formData).then((res) => {
            if (res && res.data) {

                commit("SET_DICTS", res.data);

                return res;
            }
        });
    },
    //编辑字典库菜单列表 
    editDict({ commit }, formData) {
        return editDict(formData).then((res) => {
            if (res && res.data) {

                commit("SET_DICTS", res.data);

                return res;
            }
        });
    },
}
export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}